package isi_graf;

import isi_ib.barra_trade;
import isi_ib.barras_trade;
import isi_ib.isi_estudios;

import java.sql.SQLException;
import java.util.Date;
import java.util.Iterator;

import javax.swing.DefaultListModel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.time.Day;
import org.jfree.data.time.Minute;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import utiles_isi.dateutil;
import utiles_isi.isi_conexion;

public class isiBenChart {
	private XYDataset dataset;
	private JFreeChart chart;
	private ChartPanel chartPanel;
	String titulo;
	String macd_properties;	
	String signal_properties;	
	String medias_properties;
	private int periodo;
	
	public  isiBenChart(String titulo_) {
		super();
		dataset=null;
		chart=null;
		chartPanel=null;
		titulo=titulo_;
		periodo=-1;
		
	}
	
	public ChartPanel CreaChart(int recorte_,String macd_properties_,String signal_properties_,String medias_properties_,int periodo_){
		
		macd_properties=macd_properties_;
		signal_properties=signal_properties_;
		medias_properties=medias_properties_;
		periodo=periodo_;
		
		dataset = createDataset(recorte_);
		chart = createChart(dataset);
//		Ajusta(double(-1000),+1000);
		chartPanel = new ChartPanel(chart);
		return chartPanel;
	}
	
	private void Ajusta(Double minvalue_,Double maxvalue_){
		if(chart==null) return;
	/*	if(minvalue_==0 && maxvalue_==0){
			minvalue_ = getLowestLow(dataset);
			maxvalue_ = getHighestHigh(dataset);
		}*/
		chart.getXYPlot().getRangeAxis().setRange(minvalue_,maxvalue_);
	}
		
	
	
	private JFreeChart createChart(XYDataset dataset_) {
			  if(dataset==null) return null; 
			/*  JFreeChart chart = ChartFactory.createXYLineChart(
			            titulo,      // chart title
			            "X",                      // x axis label
			            "Y",                      // y axis label
			            dataset,                  // data
			            PlotOrientation.VERTICAL,
			            true,                     // include legend
			            true,                     // tooltips
			            false                     // urls
			        );*/
			  
			  JFreeChart chart = ChartFactory.createTimeSeriesChart(
					  titulo,
			            "Date", 
			            "Value",
			            dataset_,
			            true,
			            true,
			            false
			        );
			  
			  
			  return chart;
			  }
	
	/*
	 private XYDataset createDataset(int recorte_) {
        String sql_="";
		XYSeriesCollection dataset = new XYSeriesCollection();	 
		XYSeries series1 = new XYSeries("Diario");
		XYSeries series2 = new XYSeries("-");
		XYSeries series3 = new XYSeries("Acumulado");
		isi_conexion con= new isi_conexion("");
		if(!con.Active()) con.conecta();
			
		
			
		barras_trade barrasa_ = new barras_trade();
		Iterator<String> itr = barrasa_.ListafechasSQL().iterator();
				
		int i=0;
		double acumulado=0;
		Double benef_=(double) 0;
		while (itr.hasNext()) {
			String element=itr.next();
			if(!element.isEmpty()){
			isi_estudios estu1 =new isi_estudios(null,null,"",false);
			estu1.IniciaMAC(macd_properties);
			estu1.Inicia_signal(signal_properties);
			estu1.IniciaMedias(medias_properties);
			estu1.IniciaAcumulado(periodo);
			estu1.cargaSql(element);
			estu1.IniciaOscilador(14,2,7,1);
			benef_=estu1.CalculaBeneficios(true,true);
			acumulado=acumulado+benef_;
			series1.add(i,benef_);
			series2.add(i,0);
			series3.add(i,acumulado);
			sql_="REPLACE INTO isitrader.calculos (params,fecha,total) VALUES ('"+macd_properties+"','"+element+"','"+benef_.toString()+"')";
			try {
				if(con.conectado)con.st.execute(sql_);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			i++;
			}
			
		}
		
		dataset.addSeries(series1);		
		dataset.addSeries(series2);	
		dataset.addSeries(series3);	
			
			  return dataset;
			  }
 
	  
	  
	 */
	
	

	
	
	
	private XYDataset createDataset(int recorte_) {
        String sql_="";
		TimeSeriesCollection dataset =new TimeSeriesCollection();
		dataset.setDomainIsPointsInTime(true);
		
		TimeSeries series1 = new TimeSeries("Diario", Day.class);
		TimeSeries series2 = new TimeSeries("-", Day.class);
		TimeSeries series3 = new TimeSeries("Acumulado", Day.class);
		
		
		isi_conexion con= new isi_conexion("");
		if(!con.Active()) con.conecta();
			
		
			
		barras_trade barrasa_ = new barras_trade();
		Iterator<String> itr = barrasa_.ListafechasSQL().iterator();
				
		int i=0;
		double acumulado=0;
		Double benef_=(double) 0;
		while (itr.hasNext()) {
			String element=itr.next();
			if(!element.isEmpty()){
			isi_estudios estu1 =new isi_estudios(null,null,"",false);
			estu1.IniciaMAC(macd_properties);
			estu1.Inicia_signal(signal_properties);
			estu1.IniciaMedias(medias_properties);
			estu1.IniciaAcumulado(periodo);
			estu1.cargaSql(element);
			estu1.IniciaOscilador(14,2,7,1);
			benef_=estu1.CalculaBeneficios(true,true);
			acumulado=acumulado+benef_;
			
			Day da_=new Day(dateutil.dateFromString("", element));
			series1.add(da_,benef_);
			series2.add(da_,0);
			series3.add(da_,acumulado);
			sql_="REPLACE INTO isitrader.calculos (params,fecha,total) VALUES ('"+macd_properties+"','"+element+"','"+benef_.toString()+"')";
			try {
				if(con.conectado)con.st.execute(sql_);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			i++;
			}
			
		}
		
		dataset.addSeries(series1);		
		dataset.addSeries(series2);	
		dataset.addSeries(series3);	
			
			  return dataset;
			  }
}
